/*
 * @Autor: dingxiaolin
 * @Date: 2020-12-02 16:26:18
 * @LastEditors: dingxiaolin
 * @LastEditTime: 2020-12-24 21:10:20
 * https://github.com/wux-weapp/wx-extend/blob/master/src/assets/plugins/wx-validate/WxValidate.js 另一个校验器
 */
const form = {
    /*  @formData 要验证的表单数据
        @rules 验证规则 required必填
    ***/
    validation(formData, rules) {
        for (let item of rules) {
            let key = item.name;
            let ruleArr = item.rule;
            let msgArr = item.msg;
            //校验规则里没有表单字段则不校验次规则
            if (!key || !(key in formData) || !ruleArr || ruleArr.length === 0 || !msgArr || msgArr.length === 0) continue //跳出循环，不校验
            for (let i = 0; i < ruleArr.length; i++) {
                let ruleItem = ruleArr[i];
                let msg = msgArr[i];
                if (!ruleItem || !msg) continue
                console.log('ruleItem',ruleItem)
                let value = null;
                if (ruleItem.indexOf(":") != -1) {
                    let temp = ruleItem.split(":");
                    ruleItem = temp[0];
                    value = temp[1];
                }

                let isError = false;
                switch (ruleItem) {
                case "required": //必填
                    isError = form.isNull(formData[key]);
                    break;
                case "isSpace": // 全为空格
                    isError = form.isSpace(formData[key]);
                    break;
                case "hasSpace": //有空格
                    isError = form.hasSpace(formData[key]);
                    break;
                case "isEmail": //邮箱
                    isError = !form.isEmail(formData[key]);
                    break;
                case "isTel":  //手机
                    isError = !form.isTel(formData[key]);
                    break;
                case "isSpecialWord": //屏蔽特殊字符
                    isError = !form.isSpecialWord(formData[key]);
                    break;
                case "minLength": //最小长度
                    isError = !form.minLength(formData[key], value)
                    break;
                case "maxLength": //最大长度
                    isError = !form.maxLength(formData[key], value)
                    break;
                case "rangeLength": //区间长度
                    isError = !form.rangeLength(formData[key], JSON.parse(value))
                    break;
                case "digits": //整数
                    isError = !form.digits(formData[key])
                    break;
                case "idcard": //身份证
                    isError = !form.idcard(formData[key])
                    break;
                case "equalto": // 判断相等，例如重复密码
                    isError = !form.equalto(formData[key], formData[value])
                    break;
                default:
                    break;
                }

                if (isError) {
                    wx.showToast({
                        title: msg,
                        icon: 'none'
                    })
                    return false
                }
            }
        }
        return true;
    },
    isNull(value) {
        if (typeof value == 'object') {
            for (let prop in value) {
                if (typeof value[prop] == 'object') {
                    for (let prop2 in value[prop]) {
                        if (value[prop][prop2] === null || value[prop][prop2] === '' || value[prop][prop2] === undefined) {
                            return true
                        } else {
                            return false
                        }
                    }
                }
            }
        }
        ///^[ ]+$/.test(value) 做是否为全空格判断
        //new RegExp('^[ ]+$').test(value) 做是否为全空格判断
        return (value === null  || value === undefined || value === '' || new RegExp('^[ ]+$').test(value)) ? true : false;
    },
    isEmail(value) {
        return /^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(value);
    },
    isTel(value) {
        ///^1[34578]\d{9}$/.test(value)
        return /^(?:13\d|14\d|15\d|16\d|17\d|18\d|19\d)\d{5}(\d{3}|\*{3})$/.test(value);
    },
    isSpace(value) {
        return /^[ ]+$/.test(value);
    },
    hasSpace(value) {
        return value.replace(/(^s*)|(s*$)/g, "").length > 0 ? true : false ;
    },
    isSpecialWord(value) {
        //是否包含特殊字符
        let regEn = /[`~!@#$%^&*()_+<>?:"{},.\/;'[\]]/,
            regCn = /[·！#￥（——）：；“”‘、，|《。》？、【】[\]]/;
        return (regEn.test(value) || regCn.test(value)) ? false : true
    },
    minLength(value, min) {
        return value.length >= Number(min)
    },
    maxLength(value, max) {
        return value.length <= Number(max)
    },
    rangeLength(value, param) {
        return value.length >= param[0] && value.length <= param[1]
    },
    equalto(value, value1) {
        return value===value1
    },
    idcard(value) {
        return /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/.test(value)
    },
    digits(value) {
        return  /^\d+$/.test(value)
    },
};
module.exports = {
    validation: form.validation
};